﻿package igz.fleaxy.net.rss 
{
	import flash.events.EventDispatcher;
	
	/**	 
	 * @author Intelygenz [www.intelygenz.com]
	 * 
	 * 
	 */
	 
	public class Rss extends EventDispatcher {

		[ArrayElementType("igz.fleaxy.net.rss.RssChannel")]
		protected var _Channels:Array;
		public function get $Channels():Array {
			return _Channels;
		}
		protected var _UserName:String;
		protected var _Password:String;
		protected var _Status:Number;

		public function Rss() : void {
			_Channels = [];
		}

		/**
		 *  Detecta el tipo de RSS que se recibe por parámetro y lo trata mediante RssChannel
		 * 
		 * @param	p_xml XML con los datos del RSS
		 */
		public function $Load( p_xml:XML ) : void {
			// Miramos que tipo de XML nos viene (rss o atom)
			var rssType : int;
			if ( p_xml.localName() == "feed" ) {
				rssType = RssTypes.$ATOM;
				_Channels.push( new RssChannel( p_xml, rssType ) );
			}
			else if ( p_xml.localName() == "RDF" ) {
				rssType = RssTypes.$RDF;
				_Channels.push( new RssChannel( p_xml, rssType ) );
			}
			else {
				rssType = RssTypes.$RSS;
				var xmlChannel:XML;	
				for each ( xmlChannel in  p_xml.* ) {
					_Channels.push( new RssChannel( xmlChannel, rssType ) );
				}
			}
		}
		
		public function $Clear () : void {
			_Channels = new Array();
		}

		/*
		public function LoadFromURL(p_url:String):void {
			var request:URLRequest = new URLRequest( p_url );
			if ( _UserName != null ){
				var credentials:String = Base64.encode( _UserName + ":" + _Password );
				var authHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + credentials);   
				var headers:Array = [];
				headers.push(authHeader);
				request.requestHeaders = headers;
			}

			request.method = URLRequestMethod.GET;
//			request.data = "1";

			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;

			loader.addEventListener( Event.COMPLETE, _OnLoadXML ); 
			loader.addEventListener( IOErrorEvent.IO_ERROR, _OnIOError );
			loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, _OnChangeStatus );

			loader.load( request );	
		}
		*/
		
	}
}